home *** CD-ROM | disk | FTP | other *** search
/ Delphi Informant Complete 1995 - 2000 / Delphi Informant Complete 1995 to 2000.iso / Delphi Informant Magazine Complete Works SOURCE CODE 1998.rar / 1998 / Dec / di9812me / PluginSample / 2 / shell2 / main.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1998-03-24  |  2.5 KB  |  110 lines

  1. unit main;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   Menus, StdCtrls, Buttons;
  8.  
  9. const
  10.     cPLUGIN_MASK    = '*.plg';
  11.  
  12. type
  13.   TfrmMain = class(TForm)
  14.     mnuMain: TMainMenu;
  15.     File1: TMenuItem;
  16.     Exit1: TMenuItem;
  17.     GroupBox1: TGroupBox;
  18.     memPlugins: TMemo;
  19.     BitBtn1: TBitBtn;
  20.     Plugin1: TMenuItem;
  21.     procedure Exit1Click(Sender: TObject);
  22.     procedure FormCreate(Sender: TObject);
  23.   private
  24.     { Private declarations }
  25.     procedure LoadPlugins;
  26.     procedure LoadPlugin(sr: TSearchRec);
  27.   public
  28.     { Public declarations }
  29.   end;
  30.  
  31. var
  32.   frmMain: TfrmMain;
  33.  
  34. implementation
  35.  
  36. uses Common;
  37.  
  38. {$R *.DFM}
  39.  
  40. procedure TfrmMain.Exit1Click(Sender: TObject);
  41. begin
  42.     frmMain.Close;
  43. end;
  44.  
  45. {Iterate the application directory looking for plugin files}
  46. procedure TfrmMain.LoadPlugins;
  47. var sr: TSearchRec;
  48.     path: string;
  49.     Found: integer;
  50. begin
  51.     path := ExtractFilePath(Application.Exename);
  52.     try
  53.         //Found := FindFirst(path+cPLUGIN_MASK, faAnyFile, sr);
  54.         Found := FindFirst(path+cPLUGIN_MASK, 0, sr);
  55.         while Found = 0 do
  56.         begin
  57.             LoadPlugin(sr);
  58.             Found := FindNext(sr);
  59.         end;
  60.     finally
  61.         FindClose(sr);
  62.     end;
  63. end;
  64.  
  65. {Load the specified plugin DLL}
  66. procedure TfrmMain.LoadPlugin(sr: TSearchRec);
  67. var Description: String;
  68.     LibHandle: integer;
  69.     DescribeProc: TPluginDescribe;
  70.     InitProc: TPluginInit;
  71. begin
  72.     LibHandle := LoadLibrary(Pchar(sr.Name));
  73.     if LibHandle <> 0 then
  74.     begin
  75.         // Find DescribePlugin
  76.         DescribeProc := GetProcAddress(LibHandle, cPLUGIN_DESCRIBE);
  77.         if assigned(DescribeProc) then
  78.         begin
  79.             // Call DescribePlugin
  80.             DescribeProc(Description);
  81.             memPlugins.Lines.Add(Description);
  82.             // Find InitPlugin
  83.             InitProc := GetProcAddress(LibHandle, cPLUGIN_INIT);
  84.             if assigned(InitProc) then
  85.             begin
  86.                 //Call InitPlugin
  87.                 InitProc(mnuMain);
  88.             end;
  89.         end
  90.         else
  91.         begin
  92.             Messagedlg('File "'+sr.Name+'" is not a valid plugin.',
  93.                         mtInformation, [mbOK], 0);
  94.         end;
  95.     end
  96.     else
  97.     begin
  98.         Messagedlg('An error occurred loading the plugin "'+sr.Name+'".',
  99.                     mtInformation, [mbOK], 0);
  100.     end;
  101. end;
  102.  
  103.  
  104. procedure TfrmMain.FormCreate(Sender: TObject);
  105. begin
  106.     LoadPlugins;
  107. end;
  108.  
  109. end.
  110.